var color=$.cookie("color");
let dev=false;
if(location.href.indexOf("dev=yes")>=0){
    $.cookie("dev","yes");
    location.href="/";
    dev=true;
}
if ($.cookie("dev")==="yes"){
    dev=true;
}
$(function () {
    if (dev){
        $("div.footer>p:nth-child(1)").html("Developed by SCH<br><b class='text-auto-color margin-top margin-no-bottom' style='font-weight: bolder' onclick='exitDevMode()'>Develop Mode</b>");
    }
    if (!color){
        color="blue";
    }
    $("body").attr("class","page-"+color).prepend("<div id='SelectionColorControl'></div>");
    loadColor();

    $(".setColor").click(function () {
        console.log("setColor");
        color = $(this).attr("data-color");
        $("body").attr("class","page-"+color);
        loadColor();
        $.cookie("color",color);
    });
    if (!isMobile()){
        $(".heading").addClass("margin-no-top");
    }
});

function loadColor() {
    let mainColor = $("body").attr("class").split("-")[1];
    let colorList = ["blue","green","purple","red","yellow"];
    let colorCodeList = {"blue":"#2196f3","green":"#8bc34a","purple":"#9c27b0","red":"#f44336","yellow":"#ffc107"};
    let $btn = $(".btn,b.btn,button");
    let $fbtn = $(".fbtn.fbtn-lg");
    let $fg = $(".form-group");
    let $card = $(".card");
    let $text = $(".card-heading,b.suAdmin,.text-auto-color");
    let $switch = $(".switch-toggle");
    let $selection = $("#SelectionColorControl");
    try {
        let x = $(".circle-icon");
        for (const k in x) {
            x[k].style.backgroundColor = colorCodeList[mainColor];
        }
        x = undefined;
        x = document.getElementsByClassName("circle");
        for (const k in x) {
            console.log(x[k]);
            x[k].style.borderColor = colorCodeList[mainColor];
        }
    }catch{}
    for (const colorListKey in colorList) {
        let c = colorList[colorListKey];
        $btn.removeClass("btn-"+c);
        $fbtn.removeClass("fbtn-"+c);
        $fg.removeClass("form-group-"+c);
        $card.removeClass("card-"+c);
        $text.removeClass("text-"+c);
        $switch.removeClass("switch-toggle-"+c);
    }
    $btn.addClass("btn-"+mainColor);
    $fbtn.addClass("fbtn-"+mainColor);
    $fg.addClass("form-group-"+mainColor);
    $card.addClass("card-"+mainColor);
    $text.addClass("text-"+mainColor);
    $switch.addClass("switch-toggle-"+mainColor);
    $selection.html("<style>*::selection{background-color: "+colorCodeList[mainColor]+";color: white;}div.content-heading>div.container>.heading::selection{background-color: white;color: "+colorCodeList[mainColor]+"}</style>")
}

function isMobile(){
    let info = navigator.userAgent;
    let agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPod", "iPad"];
    for(let i = 0; i < agents.length; i++){
        if(info.indexOf(agents[i]) >= 0) return true;
    }
    return false;
}

function exitDevMode(){
    if (confirm("Are you sure to exit develop mode?")){
        $.cookie("dev","");
        location.reload();
    }
}